package com.le.tester.book.concurrentjavafight.l7_1_5;

import java.util.concurrent.*;

/**
 * createTime：2022/11/9 13:42
 * description：timedRun Future
 */
public class Demo {

    private static final ExecutorService exec = Executors.newCachedThreadPool();

    public static void timedRun(Runnable r, long timeout, TimeUnit timeUnit) throws InterruptedException {
        Future<?> submitTask = exec.submit(r);
        try {
            submitTask.get(timeout, timeUnit);
        } catch (TimeoutException e) {

            //走进该异常，将表明任务将会被取消
        } catch (ExecutionException e) {
            //在执行的时候抛出了该异常

        } finally {
            //bool将判断取消的时候是否立即终止
            submitTask.cancel(Boolean.FALSE);
        }
    }
}
